﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using BLLTZS.Funcionalidad;

namespace TZS
{
    public partial class MostrarImagen : FormGenerico
    {
        public string Img
        {
            get
            {
                string verga = string.Empty;

                if (Request.QueryString["img"] != null)
                    verga = Request.QueryString["img"];

                return verga;
            }
        }

        private System.Drawing.Image ConvertirEnImage(byte[] bytes)
        {
            MemoryStream ms = new MemoryStream(bytes);
            Bitmap bm = new Bitmap(ms);

            return bm;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            BLLRol bllRol = new BLLRol();

            if (Img.Contains(User.Identity.Name) || bllRol.TraerRoles(User.Identity.Name).Contains("Admin"))
            {
                string path = Server.MapPath(string.Format(URLParaDNI, Img));

                if (Img.Contains(".pdf"))
                {
                    Response.ContentType = "application/pdf";
                    Response.WriteFile(path);
                }
                else
                {
                    if (File.Exists(path))
                    {
                        byte[] bytes = File.ReadAllBytes(path);
                        System.Drawing.Image img = ConvertirEnImage(bytes);
                        Response.ContentType = @"image/png";
                        img.Save(Response.OutputStream, ImageFormat.Png);
                    }
                }
            }
        }
    }
}